/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { getMonitorList, getAllMonitorType, getAllMonitorBrand } from '@/services/allocation';
import { isLegalData } from '@/utils/utils';
import { message, Modal } from 'antd';

export default {
  namespace: 'dataMonitor',

  state: {},

  effects: {
    // 数据监控，获取扫描识别记录、特殊件操作记录列表
    *getMonitorList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getMonitorList, payload);
      const { n_export } = payload;
      if (!response) return;
      const { code, data = {}, msg } = response;

      if (code === 0 && data) {
        const { total, page, list = [] } = data;
        if (n_export) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
          return;
        }
        __dva_resolve({
          list: isLegalData(list),
          pagination: {
            total,
            pageSize: 20,
            current: page,
            showQuickJumper: false,
            showSizeChanger: false,
          },
        });
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 共配，数据监控，获取所有类型
    *getAllMonitorType({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getAllMonitorType, payload);
      if (!response) return;
      const { code, data = [], msg } = response;

      if (code === 0 && data) {
        __dva_resolve(isLegalData(data, []));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 共配，数据监控，获取所有支持的品牌
    *getAllMonitorBrand({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getAllMonitorBrand, payload);
      if (!response) return;
      const { code, data = [], msg } = response;

      if (code === 0 && data) {
        __dva_resolve(isLegalData(data, []));
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
